<%@ page import="com.tianxiabuyi.utils.Tools" %>
<%@ page import="com.tianxiabuyi.utils.JSONUtil" %>
<%@ page import="com.alibaba.fastjson.TypeReference" %>
<%@ page pageEncoding="UTF-8"%>
<%!
    // uid加时间戳生成字符串
    public static String encode(Integer uid) {
        String result = new Date().getTime() + "";
        Random r = new Random();
        char[] chs = result.toCharArray();
        int length = chs.length;
        int count = (uid+"").length();
        String randoms = new String();
        String str = "";
        for (int i = 0; i < count; i++) {
             Integer random = r.nextInt(length);
             while (randoms.contains(Integer.toHexString(random))) {
            	 random = r.nextInt(length);
             }
             randoms += Integer.toHexString(random);
             str += chs[random];
             chs[random] = (uid+"").charAt(i);
        }
        StringBuilder sb = new StringBuilder(randoms);
        sb.append(chs, 0, length);
        sb.append(str);
        return sb.toString();
    }

    // 字符串解析uid
    public String decode(String result) {
        try {
            String randoms = result.substring(0, (result.length() - 13) / 2);
            String content = result.substring((result.length() - 13) / 2, result.length() - (result.length() - 13) / 2);
            String str =  result.substring(result.length() - (result.length() - 13) / 2, result.length());
            StringBuilder sb = new StringBuilder();
            StringBuilder time = new StringBuilder();
            char[] chs = content.toCharArray();
            for (int i = 0; i < randoms.length(); i++) {
            	char random = randoms.charAt(i);
            	int r = Integer.parseInt(random+"", 16);
            	sb.append(chs[r]);
            	chs[r] = str.charAt(i);
            }
            time.append(chs, 0, content.length());
            long date = new Date().getTime();
            long last = Long.parseLong(time.toString());
            return (date - last) / 1000 / 60 > 5 ? "past" : sb.toString();
        } catch (Exception e) {
            return "";
        }
    }
	
	// 生成二维码内容,永远有效, type:patient,doctor,dept
	public String encodeQrCode(Integer id, String type) {
		return encodeQrCode(id, type, -1);
	}
	
	// 生成二维码内容,如果是永远有效expirein传-1
	public String encodeQrCode(Integer id, String type, Integer expirein) {
		Map<String, Object> code = new HashMap<>();
		code.put("id", id + "");
		code.put("type", type);
		code.put("expirein", expirein + "");
		code.put("create_time", System.currentTimeMillis() + "");
		return Tools.encryptStr(JSONUtil.toJson(code));
	}
	
	// 解析二维码内容
	public Message decodeQrCode(String source) {
		Message msg = new Message();
		try {
			Map<String, Object> code = JSONUtil.fromJson(Tools.decryptStr(source), new TypeReference<Map<String, Object>>(){});
			if (isEmpty(code)) {
				msg.setErrcodeAndErrmsg(-1, "二维码不合法");
				return msg;
			}
			Integer expirein = Tools.getInt(code.get("expirein") + "");
			if (expirein == -1) {
				msg.put("code", code);
				return msg;
			}
			Long now = System.currentTimeMillis();
			Long codeValidTime = Tools.getLong(code.get("create_time") + "") + expirein * 1000;
			if (now > codeValidTime) {
				msg.setErrcodeAndErrmsg(-2, "二维码已失效");
				return msg;
			}
			msg.put("code", code);
		} catch(Exception e) {
			msg = new Message(30041, "请扫描合法的患者、医生或科室二维码");
		}
		return msg;
	}
%>
